package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Groups;
import com.bmp.service.GroupsManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GroupsAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private GroupsManager groupsManager;
	private Groups groups;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	
	public GroupsAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		groupsManager = (GroupsManager) ctx.getBean("groupsManager");
	}
	
	@Override
	public String execute() throws Exception{
		Groups g = new Groups();
		ActionContext.getContext().getSession().put("groups", g);
		return "success";
		
	}
	
	//显示所有组信息-JSON格式
	public String groupsInfo(){
		System.out.println("hello groupsInfo");
		//System.out.println("rows="+rows);
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;  
        List<Groups> groupsList = groupsManager.getGroups(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", groupsManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", groupsList);//rows键 存放每页记录 list  
        //System.out.println("intPage="+intPage);
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        //System.out.println("number="+number);
		return "groupsInfo";
	}
	
	//增加组
	public String addGroups(){
		groupsManager.addGroups(this.groups);
		return "success";
	}
	
	//删除组
	public String deleteGroups(){
		groupsManager.deleteGroupsById(this.groups.getId());
		return "success";
	}
	
	//更新组信息
	public String updateGroups(){
		groupsManager.updateGroups(this.groups);
		return "success";
	}
	
	public String loadGroups(){
		groupsManager.getGroupsById(this.groups.getId());
		return "load";
	}
	
	/*************getter and setter************************/
	public GroupsManager getGroupsManager() {
		return groupsManager;
	}
	public void setGroupsManager(GroupsManager groupsManager) {
		this.groupsManager = groupsManager;
	}
	public Groups getGroups() {
		return groups;
	}
	public void setGroups(Groups groups) {
		this.groups = groups;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
}
